// insight_script.js

// 模块A菜单区 =================================================================================
document.addEventListener('DOMContentLoaded', () => {
    // 首页按钮点击事件
    document.getElementById('home-btn').addEventListener('click', () => {
        window.location.href = 'home.html'; // 跳转到首页
    });

    // 显示或隐藏下拉菜单
    document.getElementById('settings-btn').addEventListener('click', () => {
        const dropdown = document.getElementById('settings-dropdown');
        dropdown.style.display = dropdown.style.display === 'block' ? 'none' : 'block';
    });

    document.getElementById('favorite-btn').addEventListener('click', () => {
        const dropdown = document.getElementById('favorite-dropdown');
        dropdown.style.display = dropdown.style.display === 'block' ? 'none' : 'block';
    });

    // 隐藏下拉菜单
    window.addEventListener('click', (event) => {
        const settingsDropdown = document.getElementById('settings-dropdown');
        const favoriteDropdown = document.getElementById('favorite-dropdown');

        if (!event.target.closest('.nav-links')) {
            settingsDropdown.style.display = 'none';
            favoriteDropdown.style.display = 'none';
        }
    });
});

// 模块B问答区 =================================================================================
// 智能问答处理
function handleQuestion() {
    const input = document.getElementById('question-input');
    const question = input.value.trim();
    if(!question) return;

    const chatHistory = document.getElementById('chat-history');

    // 用户消息
    chatHistory.innerHTML += `
        <div class="chat-message user-message">
            ${question}
        </div>
    `;

    // 系统回复
    setTimeout(() => {
        chatHistory.innerHTML += `
            <div class="chat-message system-message">
                <i class="fas fa-robot" style="color: #4169E1;"></i>
                ${getAnswer(question)}
            </div>
        `;
        chatHistory.scrollTop = chatHistory.scrollHeight;
    }, 500);

    input.value = '';
}

// 模拟智能回复
function getAnswer(question) {
    const responses = {
        '家庭': '已发现5,382个家庭群组，平均ARPU提升空间23%，建议在营销全家享套餐、升级家庭安防套餐、推荐儿童定位手表等方面经营该群体。',
        '高价值': '识别到11,203位信用分优秀的高价值用户，建议在升级5G权益、推荐漫游套餐等方面经营此类用户，交叉销售机会率62%。',
        '流失': '检测到3892位潜在流失用户，建议在定期回访关怀、优惠券激励发放等方面立即干预。',
        '新增': '查询到本月6776个新增用户的信息，以全球通套餐办理入网为主，可能是本月新上的套餐较受欢迎，建议营销长期合约与本网宽带。',
        '专业公司产品': '经过分析，最受欢迎的专业公司产品前三位是咪咕视频、移动云盘、和包支付。'
    };
    
    // 关键词部分匹配
    const matchedKey = Object.keys(responses).find(key => 
        question.toLowerCase().includes(key.toLowerCase())
    );
    
    return matchedKey ? responses[matchedKey] : '已发现相关用户群体，详见右侧分析面板';
}

// 调整模型设置
function adjustModelSettings(setting) {
    alert(`调整${setting}设置`);
}

// 显示收藏的洞察
function showFavoriteInsights() {
    alert('显示收藏的洞察');
}

// 显示历史洞察
function showHistoryInsights() {
    alert('显示历史洞察');
}

function fillInput(text) {
    const input = document.getElementById('question-input');
    input.value = text;
    input.focus(); // 聚焦输入框
}


// 模块C展示区 =================================================================================

// 家庭圈关系图
const familyNodes = new vis.DataSet([
    { id: 1, label: '主卡用户-A', group: 'master', color: getRandomColor() },
    { id: 2, label: '副卡用户\n(儿童手表)', group: 'sub', color: getRandomColor(), parentId: 1, expanded: true, hidden: false },
    { id: 3, label: '副卡用户\n(银发终端)', group: 'sub', color: getRandomColor(), parentId: 1, expanded: true, hidden: false },
    { id: 4, label: '家庭宽带', group: 'device', color: getRandomColor(), parentId: 1, expanded: true, hidden: false },
    { id: 5, label: '智能摄像头', group: 'device', color: getRandomColor(), parentId: 4, expanded: true, hidden: false },
    { id: 6, label: '宽带用户-B', group: 'device', color: getRandomColor(), parentId: 4, expanded: true, hidden: false },
    { id: 7, label: '亲情网用户-C', group: 'device', color: getRandomColor(), parentId: 4, expanded: true, hidden: false },
    { id: 8, label: '魔百和', group: 'device', color: getRandomColor(), parentId: 4, expanded: true, hidden: false },
    { id: 9, label: '智慧社区', group: 'device', color: getRandomColor(), parentId: 4, expanded: true, hidden: false },
]);

const familyEdges = new vis.DataSet([
    { from: 1, to: 2, label: '主副卡绑定' },
    { from: 1, to: 3, label: '主副卡绑定' },
    { from: 1, to: 4, label: '融合套餐' },
    { from: 4, to: 5, label: '设备绑定' },
    { from: 6, to: 4, label: '安装' },
    { from: 6, to: 7, label: '绑定' },
    { from: 6, to: 8, label: '设备绑定' },
    { from: 7, to: 9, label: '参与' },
    { from: 5, to: 9, label: '集成' },
    { from: 3, to: 9, label: '参与' },
    { from: 3, to: 8, label: '参与' }
]);

// 高价值用户特征图
const vipNodes = new vis.DataSet([
    { id: 1, label: '高价值用户', group: 'user', color: getRandomColor() },
    { id: 2, parentId: 1, label: 'ARPU>300元', group: 'tag', color: getRandomColor() },
    { id: 3, parentId: 1, label: '5G尊享套餐', group: 'tag', color: getRandomColor() },
    { id: 4, parentId: 1, label: '国际漫游', group: 'tag', color: getRandomColor() },
    { id: 5, parentId: 1, label: '3+增值业务', group: 'tag', color: getRandomColor() }
]);

const vipEdges = new vis.DataSet([
    { from: 1, to: 2 },
    { from: 1, to: 3 },
    { from: 1, to: 4 },
    { from: 1, to: 5 }
]);

// 流失预警用户图
const churnNodes = new vis.DataSet([
    { id: 1, label: '预警用户', group: 'user', color: getRandomColor() },
    { id: 2, label: '7天零流量', group: 'warning', color: getRandomColor() },
    { id: 3, label: '套餐降级', group: 'warning', color: getRandomColor() },
    { id: 4, label: '余额<10元', group: 'warning', color: getRandomColor() },
    { id: 5, label: '终端老旧', group: 'warning', color: getRandomColor() }
]);

const churnEdges = new vis.DataSet([
    { from: 1, to: 2 },
    { from: 1, to: 3 },
    { from: 1, to: 4 },
    { from: 1, to: 5 }
]);

// 新用户行为图
const newUserNodes = new vis.DataSet([
    { id: 1, label: '新用户', group: 'user', color: getRandomColor() },
    { id: 2, label: '线上入网', group: 'behavior', color: getRandomColor() },
    { id: 3, label: '首充礼包', group: 'behavior', color: getRandomColor() },
    { id: 4, label: '流量超限', group: 'behavior', color: getRandomColor() },
    { id: 5, label: '家庭组网', group: 'behavior', color: getRandomColor() }
]);

const newUserEdges = new vis.DataSet([
    { from: 1, to: 2, label: '渠道' },
    { from: 2, to: 3, label: '动作' },
    { from: 3, to: 4, label: '结果' },
    { from: 4, to: 5, label: '转化' }
]);

// 增值业务用户图
const valueAddedNodes = new vis.DataSet([
    { id: 1, label: '活跃用户', group: 'user', color: getRandomColor() },
    { id: 2, label: '咪咕视频\n(45min/日)', group: 'service', color: getRandomColor() },
    { id: 3, label: '移动云盘\n(5次/周)', group: 'service', color: getRandomColor() },
    { id: 4, label: '和包支付\n(8次/月)', group: 'service', color: getRandomColor() },
    { id: 5, label: '多屏终端', group: 'device', color: getRandomColor() }
]);

const valueAddedEdges = new vis.DataSet([
    { from: 1, to: 2 },
    { from: 1, to: 3 },
    { from: 1, to: 4 },
    { from: 1, to: 5 }
]);

// 初始化五个图实例
const network1 = initGraph('network1', familyNodes, familyEdges);
const network2 = initGraph('network2', vipNodes, vipEdges);
const network3 = initGraph('network3', churnNodes, churnEdges);
const network4 = initGraph('network4', newUserNodes, newUserEdges);
const network5 = initGraph('network5', valueAddedNodes, valueAddedEdges);


// 折叠面板功能
function toggleBox(boxNumber) {
    const box = document.querySelectorAll('.box-content')[boxNumber-1];
    box.style.display = box.style.display === 'none' ? 'grid' : 'none';
}




